/*
 * mystdlib.c
 *
 *  Created on: Sep 4, 2013
 *      Author: Ken Arok
 *
 *      Revision history:
 *      ---------------------------------------------------------
 *      Date			|	Revised by		|	Description
 *      ---------------------------------------------------------
 *      1. Sep 4, 2013	|	Yosef			| New establishment
 *
 *
 *
 * \file
 *
 * \brief Common standard library utilities.
 *
 * Copyright (c) 2013 PT Hanindo Automation Solutions. All rights reserved.
 *
 */


#include "mystdlib.h"


static const uint8_t HEX_DIGITS[16] = "0123456789ABCDEF";

/** \brief Convert unsigned char value to binary ascii
 *
 * \param _val		Value to be converted.
 * \param _string	Pointer to buffer where ascii result stored.
 *
 */
static void _mylib_uint8_to_bin(uint8_t _val, char *_string);

/** \brief Convert unsigned short value to binary ascii
 *
 * \param _val		Value to be converted.
 * \param _string	Pointer to buffer where ascii result stored.
 *
 */
static void _mylib_uint16_to_bin(uint16_t _val, char *_string);

/** \brief Convert unsigned long value to binary ascii
 *
 * \param _val		Value to be converted.
 * \param _string	Pointer to buffer where ascii result stored.
 *
 */
static void _mylib_uint32_to_bin(uint32_t _val, char *_string);

/** \brief Convert unsigned char value to decimal ascii
 *
 * \param _val		Value to be converted.
 * \param _string	Pointer to buffer where ascii result stored.
 *
 */
static void _mylib_uint8_to_dec(uint8_t _val, char *_string);

/** \brief Convert unsigned short value to decimal ascii
 *
 * \param _val		Value to be converted.
 * \param _string	Pointer to buffer where ascii result stored.
 *
 */
static void _mylib_uint16_to_dec(uint16_t _val, char *_string);

/** \brief Convert unsigned long value to decimal ascii
 *
 * \param _val		Value to be converted.
 * \param _string	Pointer to buffer where ascii result stored.
 *
 */
static void _mylib_uint32_to_dec(uint32_t _val, char *_string);

/** \brief Convert unsigned char value to hexadecimal ascii
 *
 * \param _val		Value to be converted.
 * \param _string	Pointer to buffer where ascii result stored.
 *
 */
static void _mylib_uint8_to_hex(uint8_t _val, char *_string);

/** \brief Convert unsigned short value to hexadecimal ascii
 *
 * \param _val		Value to be converted.
 * \param _string	Pointer to buffer where ascii result stored.
 *
 */
static void _mylib_uint16_to_hex(uint16_t _val, char *_string);

/** \brief Convert unsigned long value to hexadecimal ascii
 *
 * \param _val		Value to be converted.
 * \param _string	Pointer to buffer where ascii result stored.
 *
 */
static void _mylib_uint32_to_hex(uint32_t _val, char *_string);



void mylib_uint8_to_ascii(uint8_t _val, char *_string, uint8_t _radix)
{
	switch(_radix) {
		case 2:
			_mylib_uint8_to_bin(_val, _string);
		break;

		case 10:
			_mylib_uint8_to_dec(_val, _string);
		break;

		case 16:
			_mylib_uint8_to_hex(_val, _string);
		break;

		default: break;
	}
}

void mylib_uint16_to_ascii(uint16_t _val, char *_string, uint8_t _radix)
{
	switch(_radix) {
		case 2:
			_mylib_uint16_to_bin(_val, _string);
		break;

		case 10:
			_mylib_uint16_to_dec(_val, _string);
		break;

		case 16:
			_mylib_uint16_to_hex(_val, _string);
		break;

		default: break;
	}
}

void mylib_uint32_to_ascii(uint32_t _val, char *_string, uint8_t _radix)
{
	switch(_radix) {
		case 2:
			_mylib_uint32_to_bin(_val, _string);
		break;

		case 10:
			_mylib_uint32_to_dec(_val, _string);
		break;

		case 16:
			_mylib_uint32_to_hex(_val, _string);
		break;

		default: break;
	}
}


/* ----------------------------------------------------------------------------- */

static void _mylib_uint8_to_bin(uint8_t _val, char *_string)
{
	int8_t i;
	uint8_t __val;

	i = 0;
	__val = _val;

	do {
		if(__val & 0x80) *(_string + i) = 0x31;
		else *(_string + i) = 0x30;
		i++;
		__val = __val << 1;
	}while(i < 8);

	*(_string + i) = '\0';
}


static void _mylib_uint16_to_bin(uint16_t _val, char *_string)
{
	int8_t i;
	uint16_t __val;

	i = 0;
	__val = _val;

	do {
		if(__val & 0x8000) *(_string + i) = 0x31;
		else *(_string + i) = 0x30;
		i++;
		__val = __val << 1;
	}while(i < 16);

	*(_string + i) = '\0';
}

static void _mylib_uint32_to_bin(uint32_t _val, char *_string)
{
	int8_t i;
	uint32_t __val;

	i = 0;
	__val = _val;

	do {
		if(__val & 0x8000) *(_string + i) = 0x31;
		else *(_string + i) = 0x30;
		i++;
		__val = __val << 1;
	}while(i < 32);

	*(_string + i) = '\0';
}




static void _mylib_uint8_to_dec(uint8_t _val, char *_string)
{
	int8_t i;
	uint8_t __val;

	i = 3;
	__val = _val;

	do {
		i--;
		*(_string + i) = '0' + __val % 10;
		__val = __val / 10;
	}while(i);

	*(_string + 3) = '\0';


	while(*(_string + 0) == '0') {
		i = 0;
		do {
			*(_string + i) = *(_string + i + 1);
			i++;
		}while(i < 3);
	};

	if(*(_string + 0) == 0) *(_string + 0) = '0';
}


static void _mylib_uint16_to_dec(uint16_t _val, char *_string)
{
	int8_t i;
	uint16_t __val;

	i = 5;
	__val = _val;

	do {
		i--;
		*(_string + i) = '0' + __val % 10;
		__val = __val / 10;
	}while(i);

	*(_string + 5) = '\0';


	while(*(_string + 0) == '0') {
		i = 0;
		do {
			*(_string + i) = *(_string + i + 1);
			i++;
		}while(i < 5);
	};

	if(*(_string + 0) == 0) *(_string + 0) = '0';
}


static void _mylib_uint32_to_dec(uint32_t _val, char *_string)
{
	int8_t i;
	uint32_t __val;

	i = 10;
	__val = _val;

	do {
		i--;
		*(_string + i) = '0' + __val % 10;
		__val = __val / 10;
	}while(i);

	*(_string + 10) = '\0';


	while(*(_string + 0) == '0') {
		i = 0;
		do {
			*(_string + i) = *(_string + i + 1);
			i++;
		}while(i < 10);
	};

	if(*(_string + 0) == 0) *(_string + 0) = '0';
}



static void _mylib_uint8_to_hex(uint8_t _val, char *_string)
{
	int8_t i;
	uint8_t __val;

	i = 0;
	__val = _val;

	for(i = 1; i >= 0; i--) {
		*(_string + i) = HEX_DIGITS[__val & 0x0F];
		__val = __val >> 4;
	}
	*(_string + 2) = '\0';
}


static void _mylib_uint16_to_hex(uint16_t _val, char *_string)
{
	int8_t i;
	uint16_t __val;

	i = 0;
	__val = _val;

	for(i = 3; i >= 0; i--) {
		*(_string + i) = HEX_DIGITS[__val & 0x000F];
		__val = __val >> 4;
	}
	*(_string + 4) = '\0';
}


static void _mylib_uint32_to_hex(uint32_t _val, char *_string)
{
	int8_t i;
	uint32_t __val;

	i = 0;
	__val = _val;

	for(i = 7; i >= 0; i--) {
		*(_string + i) = HEX_DIGITS[__val & 0x0000000F];
		__val = __val >> 4;
	}
	*(_string + 8) = '\0';
}


